import axios from "axios";

export default {
  test() {
    axios.get('/api').then(response => { console.log(response) });
  },
  login(context, payload) {
    context.commit('setLoading', true);
    axios.post('api/', { action: 'login', payload: payload }).then(response => {
      if (context.state.isDev) console.log(response.data);
      context.commit('login', response.data.status);
      context.commit('setLoading', false);
      if (response.data.status == 'success') {
        context.commit('setLoginData', payload);
        context.commit('updateData', {
          securityData: response.data.security_data[0],
          basicData: response.data.basic_data[0],
          extentionData: response.data.extention_data,
          dataConfig: response.data.data_conf,
        });
        if (payload.adminPIMS != "0") {
          context.dispatch('loadAdminContent');
        }
      }
    });
  },
  loadVerifyData(context) {
    context.commit("setLoading", true);
    return new Promise((resolve, reject) => {
      axios
        .post("api/", {
          action: "get_verify_list",
          payload: context.state.loginData
        })
        .then(response => {
          window.console.log("loadVerifyData");
          window.console.log(response.data);
          let payload = {
            status: response.data.result,
            request: response.data.content,
            discard: response.data.discard
          };
          context.commit("setVerifyData", payload);
          context.commit("setLoading", false);
          resolve();
        }).catch(error => {
          context.commit("setLoading", false);
          console.log(error);
          reject(error);
        });
    });
  },
  operateVerify(context, payload) {
    let yes = payload.yes;
    let data = payload.data;
    context.commit("setLoading", true);
    let payload_new = {
      login_data: context.state.loginData,
      content: context.state.verifyData.request[data.index],
      value_old: context.state.verifyData.discard[data.index],
      freetext: data.freetext
    };
    let action = "refusal";
    if (yes) action = "approval";
    let santData = {
      action: action,
      payload: payload_new
    };
    window.console.log(santData);
    return new Promise((resolve, reject) => {
      axios
        .post("api/", santData)
        .then(response => {
          window.console.log(response.data);
          context.dispatch("loadVerifyData").then(() => {
            context.commit("setLoading", false);
            resolve();
          }).catch(error => {
            context.commit("setLoading", false);
            console.log(error);
            reject(error);
          });
        }).catch(error => {
          context.commit("setLoading", false);
          console.log(error);
          reject(error);
        });
    })
  },
  getStaffList(context) {
    axios
      .post("api/", {
        action: "get_staff_list",
        payload: { login_data: context.state.loginData }
      })
      .then(response => {
        window.console.log(response.data.content);
        context.commit("setStaffList", response.data.content);
      });
  },
  getOperateLog(context) {
    context.commit("setLoading", true);
    axios
      .post("api/", {
        action: "get_operate_log",
        payload: { login_data: context.state.loginData }
      })
      .then(response => {
        window.console.log("getOperateLog");
        window.console.log(response.data);
        context.commit("setOperateLog", response.data.content);
        context.commit("setLoading", false);
      });

  },
  getFullData(context) {
    context.commit("setLoading", true);
    axios
      .post("api/", {
        action: "export",
        payload: { login_data: context.state.loginData }
      })
      .then(response => {
        window.console.log(response.data);
        context.commit("setFullData", response.data.content);
        context.commit("setLoading", false);
      });
  },
  modifyItem(context, payload) {
    context.commit("setLoading", true);
    let postdata = {
      action: "modify_single_data",
      payload: {
        login_data: context.state.loginData,
        content: payload
      }
    };
    axios.post('api/', postdata).then(response => {
      window.console.log(postdata);
      window.console.log(response.data);
      if (response.data.result == "success") {
        context.commit("modifyItem", payload);
      }
      context.commit("setLoading", false);
    });
  },
  deleteItem(context, payload) {
    context.commit("setLoading", true);
    let postdata = {
      action: "delete_single_data",
      payload: {
        login_data: context.state.loginData,
        content: payload
      }
    };
    axios.post('api/', postdata).then(response => {
      window.console.log(postdata);
      window.console.log(response.data);
      if (response.data.result == "success") {
        context.commit("deleteItem", payload);
      }
      context.commit("setLoading", false);
    });
  },
  insertItem(context, payload) {
    context.commit("setLoading", true);
    let postdata = {
      action: "insert_single_data",
      payload: {
        login_data: context.state.loginData,
        content: payload
      }
    };
    axios.post('api/', postdata).then(response => {
      window.console.log(postdata);
      window.console.log(response.data);
      if (response.data.result == "success") {
        context.commit("insertItem", payload);
      }
      context.commit("setLoading", false);
    });
  },
  resetPasswd(context, payload) {
    context.commit("setLoading", true);
    let postdata = {
      action: "reset_passwd",
      payload: {
        login_data: context.state.loginData,
        content: payload
      }
    };
    axios.post('api/', postdata).then(response => {
      window.console.log(postdata);
      window.console.log(response.data);
      context.commit("setLoading", false);
    });
  },
  loadNotifySource(context) {
    context.commit("setLoading", true);
    axios.post('api/get_notify_source.php').then(response => {
      context.commit("setNotifySource", response.data);
      console.log(response.data)
      context.commit("setLoading", false);

    }).catch(error => {
      console.log(error)
      context.commit("setLoading", false);
    });
  },
  loadParams(context) {
    context.commit("setLoading", true);
    axios.post('api/get_all_params.php').then(response => {
      context.commit("setParams", response.data);
      console.log(response.data)
      context.commit("setLoading", false);
    }).catch(error => {
      console.log(error)
      context.commit("setLoading", false);
    });
  },
  updateParam(context, payload) {
    // context.commit("setLoading", true);
    let postData = {
      user: context.state.loginData,
      input: {
        name: payload.name,
        value: payload.value
      }
    };
    return new Promise((resolve, reject) => {
      axios.post('api/set_param.php', postData).then(response => {
        context.commit("setParam", {
          name: payload.name,
          value: payload.value,
        });
        console.log(response.data)
        context.commit("setLoading", false);
        resolve();
      }).catch(error => {
        console.log(error);
        context.commit("setLoading", false);
        reject(error);
      });
    })
  },
  loadAdminContent(context) {
    context.dispatch("loadParams");
    context.dispatch("getStaffList");
    context.dispatch("getFullData");
    context.dispatch("loadVerifyData");
    context.dispatch("getOperateLog");
    context.dispatch("loadNotifySource");
  }
}